/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingle.widget.utils;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

import java.util.WeakHashMap;

/**
 * 属性动画工具类
 */
public final class ViewHelper {

    private static final WeakHashMap<Component, AnimatorProperty> PROXIES = new WeakHashMap<>();

    private ViewHelper() {
    }

    /**
     * 透明动画
     *
     * @param view  控件
     * @param alpha 透明度
     */
    public static void setAlpha(Component view, float alpha) {
        wrap(view).alpha(alpha);
    }

    /**
     * 旋转动画
     *
     * @param view     控件
     * @param rotation 旋转角度
     */
    public static void setRotation(Component view, float rotation) {
        wrap(view).rotate(rotation);
    }

    /**
     * 缩放动画
     *
     * @param view   控件
     * @param scaleX X缩放度
     */
    public static void setScaleX(Component view, float scaleX) {
        wrap(view).scaleX(scaleX);
    }

    /**
     * 缩放动画
     *
     * @param view   控件
     * @param scaleY Y缩放度
     */
    public static void setScaleY(Component view, float scaleY) {
        wrap(view).scaleY(scaleY);
    }

    /**
     * 位移动画
     *
     * @param view         控件
     * @param translationX X轴位移距离，px
     */
    public static void setTranslationX(Component view, float translationX) {
        wrap(view).moveByX(translationX);
    }

    /**
     * 位移动画
     *
     * @param view         控件
     * @param translationY Y轴位移距离，px
     */
    public static void setTranslationY(Component view, float translationY) {
        wrap(view).moveByY(translationY);
    }

    /**
     * 获取一个属性动画
     *
     * @param view 控件
     * @return 该控件的属性动画对象
     */
    public static AnimatorProperty wrap(Component view) {
        AnimatorProperty proxy = PROXIES.get(view);
        if (proxy == null || proxy != view.createAnimatorProperty()) {
            proxy = new AnimatorProperty(view);
            PROXIES.put(view, proxy);
        }
        return proxy;
    }

}
